<?php
	require_once('security.php');
	
//	include('../../php-debugger/class/odebugger.cls.php');
//	$oDebugger = new odebugger ('EN');
	
	unset($cats);
	exec("ls -1A ../categories/ ",$cats);
	
	if (isset($_POST['submit'])) { // Handle the form
		unset($_POST['submit']);
		unset($catfh);

		foreach($cats as $cat){
			$catfh[$cat]= fopen("../categories/" .$cat, 'w') or die("Unable to open file: " .$ftmp);
		}

		foreach($_POST as $val){
			unset($tmparray);
			$tmparray= explode(" ",$val);
			fwrite($catfh[$tmparray[0]],$tmparray[1] ."\n") or die("Unable to write: " .$tmparray[1] ." to category file. <br />");
		} 

		foreach($catfh as $fh){
			fclose($fh) or die("Unable to close file. <br />");
		}	
		unset($cats);
		exec("ls -1A ../categories/ ",$cats);
		$response="<br /><b>All category information updated as per the above table.</b>";
		$submitcount++;
	}
?>

<?php $thisPage = "categorize"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Categorize Images</title>
<meta name="robots" content="noindex,nofollow" />
<?php require_once('theme/admintheme.php');?>
</head>
<body>

<div id="wrap">

	<?php include_once('navigation.php');

	$tmpstr="";
	foreach($cats as $cat){
		$tmpstr=$tmpstr .$cat;
	}
	if (strlen($tmpstr) < 90){  // Format with numbers if too wide
		$long=false;
	}else{
		$long=true;
	}
	$catimgarray=array();
	$cnt=0;
	foreach($cats as $cat){
		unset($tmp);
		exec("cat ../categories/" .$cat,$tmp);
		unset($tmp2);
		foreach($tmp as $pic){
			$tmp2[$pic]=true;
		}
		$catimgarray[$cat]=$tmp2;
		$cnt++;
		if ($long){ echo "<b>Category: " .$cnt ." = " .$cat ."</b><br />"; }
	}
//print_r($catimgarray);	 	
	unset($imgs);
	exec("ls -1A ../imgtxt/ |grep -F '.jpg' |sed 's^\.jpg^^' ",$imgs);

	?>
	
<br />
<table  border="1" cellspacing="8" cellpadding="22" frame="void" rules="none" >
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<tr><th>Image Name </th>
	
	<?php  
	$cnt=1;
	foreach($cats as $cat){
		if ($long){
			echo "<th>" .$cnt ."</th>";
			$cnt++;
		}else{
			echo "<th>" .$cat ."</th>";
		}
	}
	echo "</tr>";
	$cnt=0;
	foreach($imgs as $img){
		echo "<tr>";
		echo "<td>" .$img ."</td>";
		foreach($cats as $cat){
			echo "<td><input type=\"checkbox\" value=\"" .$cat ." " .$img ."\" name=\"X" .$cnt ."\" ";
			if (isset($catimgarray[$cat][$img])){
				echo "checked=\"checked\" /></td>";
			}else{
				echo "/></td>";
			}
			$cnt++;
		}
		echo "</tr>\n";		
	}  
	?>
	
<tr><td>
<input type="submit" name="submit" value="  Categorize  " />
</td></tr>
</form>
</table>
	
	<?php if (isset($response)){ echo $response; } 
	unset($response); 
	unset($_POST);
	?>
	
<div style="clear: both;"></div>
</div>

</body>
</html>
